package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicCommentTag;
import cn.wolfcode.luowowo.comment.repository.IScenicCommentTagRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.comment.service.IScenicCommentTagService;
import cn.wolfcode.luowowo.scenic.query.ScenicCommentQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.util.List;

@Service
public class ScenicCommentTagServiceImpl implements IScenicCommentTagService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IScenicCommentTagRepository repository;
    @Autowired
    private IScenicCommentService scenicCommentService;

    public ScenicCommentTag save(ScenicCommentTag scenicCommentTag) {
        return repository.save(scenicCommentTag);
    }

    @Override
    public List<ScenicCommentTag> getTop10(long scenicId) {
        // 获取这些标签出现在评论中的次数
        List<ScenicCommentTag> tagsList = repository.findByScenicId(scenicId);
        for (ScenicCommentTag tag : tagsList) {
            ScenicCommentQueryObject qo = new ScenicCommentQueryObject();
            qo.setScenicId(scenicId);
            qo.setKeyword(tag.getName());
            long totalElements = scenicCommentService.query(qo).getTotalElements();
            tag.setCount(totalElements);
        }
        return tagsList;
    }
}
